namespace AltNetUK.Models.Inputs
{
    using System;
    using System.Globalization;

    public sealed class StringValidator : IValidateText
    {
        private int _minLength;

        public int MinLength
        {
            get
            {
                return this._minLength;
            }

            set
            {
                if (0 > value)
                {
                    throw new ArgumentOutOfRangeException("value");
                }

                this._minLength = value;
            }
        }

        public ValidationResult Validate(string text)
        {
            ValidationResult result = new ValidationResult();

            if (0 != this.MinLength)
            {
                if (string.IsNullOrEmpty(text))
                {
                    result.Errors.Add(string.Format(CultureInfo.InvariantCulture, "is required", this.MinLength));
                }
                else if (text.Length < this.MinLength)
                {
                    result.Errors.Add(string.Format(CultureInfo.InvariantCulture, "must be at least {0} characters long", this.MinLength));
                }
            }

            return result;
        }
    }
}